﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Cqrs.StateMachineDesigner.CustomCode
{
    /// <summary>
    /// Interaction logic for InputBox.xaml
    /// </summary>
    public partial class AddEvent : Window
    {

        public List<DomainProperty> DomainProperties { get; private set; }

        public AddEvent()
        {
            InitializeComponent();
        }

        public bool Accepted { get; private set; }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.DomainProperties.Clear();
            foreach (PropertyObject propertyObject in grdProperties.ItemsSource)
            {
                if (propertyObject.IsChecked)
                    DomainProperties.Add(
                        new DomainProperty() { Name = propertyObject.DomainName, Type = propertyObject.DomainType });
            }
            Accepted = true;
            this.Close();
        }

        public List<DomainProperty> ShowForm(List<DomainProperty> myDomainProperties)
        {
            this.DomainProperties = myDomainProperties;
            List<PropertyObject> propertyObjects = new List<PropertyObject>();
            foreach (DomainProperty domainProperty in DesignerCanvas.AllDomainProperties)
            {
                PropertyObject propertyObject = new PropertyObject(domainProperty.Name, domainProperty.Type,
                        myDomainProperties.Exists(p => p.Name == domainProperty.Name));
                propertyObjects.Add(propertyObject);
            }
            grdProperties.ItemsSource = propertyObjects;
            Accepted = false;
            this.ShowDialog();
            return this.DomainProperties;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtCommand.Focus();
            this.txtCommand.SelectAll();
        }

        public class PropertyObject : INotifyPropertyChanged
        {
            public PropertyObject(string domainName, string domainType, bool isChecked)
            {
                DomainName = domainName;
                DomainType = domainType;
                _isChecked = isChecked;
            }
            private bool _isChecked;

            public string DomainName
            {
                get;
                private set;
            }
            public string DomainType
            {
                get;
                private set;
            }
            public bool IsChecked
            {
                get
                {
                    return _isChecked;
                }
                set
                {
                    _isChecked = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DomainProperties.Clear();
                foreach (PropertyObject propertyObject in grdProperties.ItemsSource)
                {
                    propertyObject.IsChecked = true;
                }
            }
            catch { }
        }

        private void btnSelectNone_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DomainProperties.Clear();
                foreach (PropertyObject propertyObject in grdProperties.ItemsSource)
                {
                    propertyObject.IsChecked = false;
                }
            }
            catch { }
        }

    }
}
